package _866_Prime_Palindrome;

/**
 * Created by 孙加辉 on 2018/7/22.
 */
class Solution {
    public int primePalindrome(int N) {
        int res;

        if(N==1||N==2)
            return 2;
        else if(N%2==0)
            res = N+1;
        else
            res = N;
        while(true){
            //由于偶数位的回文数字一定可以被11整除，所以不是素数
            if(res>=100000&&res<=1000000)
                res = 1000001;
            else if(res >= 10000000 && res<=100000000)
                res = 100000001;

            if(isPali(res)&&isPrime(res))
                break;
            else
                res+=2;
        }
        return res;
    }
    //判断是否为素数
    public boolean isPrime(int num){
        for(int i=3;i<=Math.sqrt(num);i++){
            if(num%i==0)
                return false;
        }
        return true;
    }
    //判断是否回文
    public boolean isPali(int num){
        int temp = num;
        int num1 = 0;
        while(temp>0){
            num1 *= 10;
            num1 += temp%10;
            temp = temp/10;
        }
        if(num==num1)
            return true;
        else
            return false;
    }
}
public class _866_Prime_Palindrome {
    public static void main(String[] args){

    }
}
